package com.example.hrsystem.data.source;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.hrsystem.data.model.TaskItem;

import java.util.List;

/**
 * 任务项数据访问对象
 * 提供对TaskItem表的操作方法
 */
@Dao
public interface TaskDao {

    /**
     * 插入一条任务记录
     */
    @Insert
    void insert(TaskItem task);

    /**
     * 更新任务记录
     */
    @Update
    void update(TaskItem task);
    
    /**
     * 删除任务记录
     */
    @Query("DELETE FROM tasks WHERE id = :id")
    void delete(int id);
    
    /**
     * 通过对象删除任务记录
     */
    @Delete
    void delete(TaskItem task);

    /**
     * 查询所有任务记录
     */
    @Query("SELECT * FROM tasks ORDER BY createTime DESC")
    LiveData<List<TaskItem>> getAllTasks();

    /**
     * 根据任务类型查询任务记录
     * taskType: 待办、待阅
     */
    @Query("SELECT * FROM tasks WHERE taskType = :taskType ORDER BY createTime DESC")
    LiveData<List<TaskItem>> getTasksByType(String taskType);

    /**
     * 根据任务状态查询任务记录
     * status: 未开始、进行中、已完成
     */
    @Query("SELECT * FROM tasks WHERE status = :status ORDER BY createTime DESC")
    LiveData<List<TaskItem>> getTasksByStatus(String status);

    /**
     * 根据优先级查询任务记录
     * priority: 低、中、高
     */
    @Query("SELECT * FROM tasks WHERE priority = :priority ORDER BY createTime DESC")
    LiveData<List<TaskItem>> getTasksByPriority(String priority);

    /**
     * 根据接收者ID查询任务记录
     */
    @Query("SELECT * FROM tasks WHERE assigneeId = :assigneeId ORDER BY createTime DESC")
    LiveData<List<TaskItem>> getTasksByAssigneeId(String assigneeId);

    /**
     * 根据创建者ID查询任务记录
     */
    @Query("SELECT * FROM tasks WHERE creatorId = :creatorId ORDER BY createTime DESC")
    LiveData<List<TaskItem>> getTasksByCreatorId(String creatorId);

    /**
     * 根据ID查询任务详情（异步版本，返回LiveData）
     */
    @Query("SELECT * FROM tasks WHERE id = :taskId")
    LiveData<TaskItem> getTaskById(int taskId);
    
    /**
     * 根据ID查询任务详情（同步版本，直接返回TaskItem对象）
     */
    @Query("SELECT * FROM tasks WHERE id = :taskId")
    TaskItem getTaskByIdSync(int taskId);

    /**
     * 查询待办事项总数
     */
    @Query("SELECT COUNT(*) FROM tasks WHERE taskType = '待办' AND assigneeId = :assigneeId")
    LiveData<Integer> getTodoTaskCount(String assigneeId);

    /**
     * 查询待阅事项总数
     */
    @Query("SELECT COUNT(*) FROM tasks WHERE taskType = '待阅' AND assigneeId = :assigneeId")
    LiveData<Integer> getReadTaskCount(String assigneeId);

    /**
     * 查询已办事项总数
     */
    @Query("SELECT COUNT(*) FROM tasks WHERE taskType = '待办' AND status = '已完成' AND assigneeId = :assigneeId")
    LiveData<Integer> getDoneTaskCount(String assigneeId);

    /**
     * 查询已阅事项总数
     */
    @Query("SELECT COUNT(*) FROM tasks WHERE taskType = '待阅' AND status = '已完成' AND assigneeId = :assigneeId")
    LiveData<Integer> getReadDoneTaskCount(String assigneeId);

    /**
     * 删除所有任务记录
     */
    @Query("DELETE FROM tasks")
    void deleteAll();
}